do "E:\yungyu\lottery_baby\do\set_environment.do"

clear
set more off

global control1 "i.pers_gender i.houhld_resid_cd pers_income_pre1 pers_asset_pre1 pers_earnings_pre1 pers_labor_pre1 married_pre1"
global control2 "totbaby_pre1"
global control3 "pre*redeem*"

use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear

foreach x in pers spouse{
	rename `x'_idn idn
	forv i = 93(1)109{
		dis "merge parent's housing information of year `i'..."
		merge m:1 year idn using "$wdata/hou_max_prop_`i'.dta", update noreport
		quiet drop if _m == 2
		drop _m
	}
	foreach y in prob_top1_vill prob_top5_vill prob_top10_vill prob_top20_vill prob_top1_town prob_top5_town prob_top10_town prob_top20_town{
		rename `y' `x'_`y'
	}
	rename idn `x'_idn 
}

foreach y in prob_top1_vill prob_top5_vill prob_top10_vill prob_top20_vill prob_top1_town prob_top5_town prob_top10_town prob_top20_town{
	gen parhou_`y' = pers_`y'
	replace parhou_`y' = spouse_`y' if spouse_`y' != . & spouse_`y' > pers_`y'
}

save "$wdata\project_lottery_fertility_b3a6_first_5k_prob.dta", replace


foreach x in village{
	use "$wdata\top1_prob_`x'.dta", clear
	loc x1 = substr("`x'",1,4)
	foreach y in top1 top5 top10{
		sum `y', d
		foreach z in 50 {
			loc `y'_`x1'_p`z' = r(p`z')
		}
	}
}

use "$wdata\project_lottery_fertility_b3a6_first_5k_prob.dta", clear

sort houseid norm

foreach x in vill {
	foreach y in top1 top5 top10{
		foreach z in 50{
			noi dis "gen parhou_prob_`y'_`x'_p`z'  = parhou_prob_`y'_`x' >= ``y'_`x'_p`z'' & parhou_prob_`y'_`x' != ."
			gen parhou_prob_`y'_`x'_p`z'  = parhou_prob_`y'_`x' >= ``y'_`x'_p`z'' & parhou_prob_`y'_`x' != .
		}
	}
}

foreach x of varlist parhou_prob_top1_vill parhou_prob_top5_vill parhou_prob_top10_vill{
	recode `x' . = 0
	replace `x' = `x'*100
}

foreach x in rlest{
	gen tot_`x' = pers_`x' + spouse_`x'
	gen v1 = tot_`x' if norm == -1
	egen pre1_`x' = mean(v1), by(houseid)
	drop v1
}

foreach x in parhou_prob_top1_vill parhou_prob_top5_vill ///
			 parhou_prob_top10_vill {
	gen `x'_preh = .
	replace `x'_preh = `x' if pre1_rlest > 0
}

gen original_norm = norm_year
replace norm_year = norm_year + 3

gen currentxprizexpost = 0
replace currentxprizexpost = prize10M if current == 1 & original_norm >= 0 

gen parent = totbaby_pre1 > 0

cap rm "$table/temp/Tab6.txt"
cap rm "$table/temp/Tab6.xls"

foreach r in top1 top5 top10 {
	
	loc y = "parhou_prob_`r'_vill"
	
	sum `y' if current == 1 & norm == 2 & parent == 1 & prize10M <= 5
	loc bm = r(mean)
	loc bm: dis %9.3f `bm'
	sum `y' if current == 0 & norm == 2 & parent == 1 & prize10M <= 5
	loc m0 = r(mean)
	sum `y' if current == 0 & norm == 9 & parent == 1 & prize10M <= 5
	loc m1 = r(mean)
	loc bt = `m1' - `m0'
	dis `bt'
	loc bt: dis %9.3f `bt'
	
	reghdfe `y' currentxprizexpost prize10M c.prize10M#i.norm c.prize10M#1.current i.age if parent == 1 & prize10M <= 5, a(norm#current year) cl(houseid)
	outreg2 using "$table/temp/Tab6.xls" , append bd(3) sd(3) nocon keep(currentxprizexpost) addtext(Baseline Mean, `bm', Baseline Trend, `bt') 
	
	reghdfe `y' currentxprizexpost prize10M c.prize10M#i.norm c.prize10M#1.current i.age $control1 $control2 $control3 if parent == 1 & prize10M <= 5, a(norm#current year) cl(houseid)
	outreg2 using "$table/temp/Tab6.xls" , append bd(3) sd(3) nocon keep(currentxprizexpost) addtext(Baseline Mean, `bm', Baseline Trend, `bt') 
	
}

import delimited using "$table/temp/Tab6.txt", clear
export excel "$table/Tables.xlsx" , sheet("Tab6") sheetreplace
